const Sequelize = require('sequelize');
const db = require('../config/db.config');
const Op = Sequelize.Op;

class baseModel {
  constructor(tableName, schema){
    this.model = db.define(tableName, schema,{freezeTableName: true,timestamps: false});
  }
  // 返回实例化的sequelize模型实例
  getModel(){
    return this.model
  }
  /**************************************查询方法**************************************/
  // 不带过滤条件的查询
  findAll(attributes){
    return attributes ? this.model.findAll({attributes: attributes}) : this.model.findAll()
  }
  findOne(attributes,where){
    return attributes ? this.model.findOne({attributes: attributes, where: where}) : this.model.findOne({where: where})
  }
  findByRelation(attributes,where,include){
    return attributes ? this.model.findAll({attributes: attributes, where: where, include:[include], raw:true}) : this.model.findAll({where: where, include:[include], raw:true})
  }
  findByRelationOrder(attributes,where, order,include){
    let orderOps = [[order, 'DESC']]
    return this.model.findAll({attributes: attributes, where: where, order: orderOps, include:[include], raw:true})
  }
  findByPk(option){
    return this.model.findByPk(option)
  }
  findByTransaction(option){
    return this.model.findAll(option)
  }
  // 带过滤条件的精确查询
  findByFilter(attributes, where){
    return attributes ? this.model.findAll({attributes: attributes, where: where}) : this.model.findAll({where: where})
  }
  // 带过滤条件的分页/限制查询
  findByFilterLimit(attributes, where, limit, offset){
    return limit && offset !== null ? this.model.findAll({attributes: attributes, where: where, limit: limit, offset:offset}) : this.model.findAll({attributes: attributes, where: where})
  }
  // 带过滤条件的排序精确查询
  findByFilterOrder(attributes, where, order){
    let orderOps = [[order, 'DESC']]
    return attributes ? this.model.findAll({attributes: attributes, where: where, order: orderOps}) : this.model.findAll({where: where, order: orderOps})
  }
  // 带过滤条件的模糊查询
  findLikeByFilter(attributes, where){
    let whereOps = {}
    for(let k in where){whereOps[k] = {[Op.like]: '%'+where[k]+'%'}}
    return attributes ? this.model.findAll({attributes: attributes, where: whereOps}) : this.model.findAll({where: whereOps})
  }
  // 带过滤条件的排序模糊查询
  findLikeByFilterOrder(attributes, where, order){
    let orderOps = [[order, 'DESC']]
    let whereOps = {}
    for(let k in where){whereOps[k] = {[Op.like]: '%'+where[k]+'%'}}
    return attributes ? this.model.findAll({attributes: attributes, where: whereOps, order: orderOps}) : this.model.findAll({where: whereOps, order: orderOps})
  }
  /**************************************更新方法**************************************/
  // 当where为null则批量更新表；当where为条件则条件更新表
  update(attributes, where) {
    return where ? this.model.update(attributes, {where: where}) : this.model.update(attributes, {where:{}})
  }
  // updateTrans(t){
  //   this.model.update({order:100}, {where:{id:4},transaction: t})
  // }
  increment(attributes,where) {
    return this.model.increment(attributes,{ where: where })
  }
  decrement(attributes,where) {
    return this.model.decrement(attributes,{ where: where })
  }
  /**************************************删除方法**************************************/
  // 条件删除
  delete(where) {
    return this.model.destroy({where: where})
  }
  /**************************************插入方法**************************************/
  // 插入单个实体
  create(entity) {
    return this.model.create(entity)
  }
  // 批量插入实体集
  createBatch(entitys, options) {
    return options ? this.model.bulkCreate(entitys, options) : this.model.bulkCreate(entitys)
  }
}

module.exports= baseModel;
